#pragma once

#include "common_data.h"

#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <semaphore.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>

#define BCSHM_NAME "bcshm"
#define BCSEM_NAME "bcsem"

#ifdef __cplusplus
extern "C" {
#endif

extern const int BCSHM_DATA_SIZES[];

enum bcshm_data_t{
    SHM_EVENT
  , SHM_ABORT
  , SHM_FCACTION
  , SHM_FLIGHT_TASK
  , SHM_FTC_TLM
  , SHM_FC_TLM
  , SHM_BC_TLM
  , SHM_MAX
};

#pragma pack(push, 1)
struct bcshmseg_t{
  EVENT_t event;
  ABORT_t abort;
  FCACTION_t fc_action;
  flight_task_t flight_task;
  
  ftc_tlm_t ftc_tlm;
  fc_tlm_t fc_tlm;
  bc_tlm_t bc_tlm;
};
#pragma pack(pop)

struct bcshm_t{
  int shm_fd;
  caddr_t* shm_ptr;
  sem_t* sem_ptr[SHM_MAX];
};

void bcshmseg_init(struct bcshmseg_t*);


void bcshm_create();

void bcshm_delete();

void bcshm_open(struct bcshm_t*);

void bcshm_close(struct bcshm_t*);

void bcshm_init();


void bcshm_read(enum bcshm_data_t, struct bcshm_t*, void*);

void bcshm_write(enum bcshm_data_t, struct bcshm_t*, void*);

void bcshmseg_print(struct bcshmseg_t*);

int bcshm_offset(enum bcshm_data_t);

#ifdef __cplusplus
};
#endif
